/*
 * TcpWritter.h
 *
 *  Created on: 2009-8-20
 *      Author: fanliangliang
 */

#ifndef TCP_WRITTER_H_
#define TCP_WRITTER_H_

#include <in_sock.h>
#include "TimeoutListener.h"

class MSocketListener;
class CTimeoutTimer;

class CTcpWriter : public CActive, public MTimeoutListener {
public:
	static CTcpWriter* NewL(MSocketListener& aListener, RSocket& aSocket);
	static CTcpWriter* NewLC(MSocketListener& aListener, RSocket& aSocket);
	virtual ~CTcpWriter();
	
	void IssueWrite(const TDesC8& aBuffer);
	
	void SetTimeout(TTimeIntervalSeconds aTimeout);
	TTimeIntervalSeconds Timeout() const;
	
protected:
	// from CActive
	virtual void DoCancel();
	virtual void RunL();
	
	// from MTimeoutListener
	virtual void OnTimerExpired();
	
private:
	CTcpWriter(MSocketListener& aListener, RSocket& aSocket);
	void ConstructL();
	void SendNextPacket();
	
private:
	enum { KWriteBufferSize = 2048 };
	
	MSocketListener& iListener;
	RSocket& iSocket;
	TBuf8<KWriteBufferSize> iTransferBuffer;
	TBuf8<KWriteBufferSize> iWriteBuffer;
	CTimeoutTimer* iTimer;
	TTimeIntervalSeconds iTimeout;
	TBool iIsSending;
};

#endif /* TCP_WRITTER_H_ */
